<link href="./themes/flat/theme.min.css" rel="stylesheet">
<link href="./css/simplebootadmin.css" rel="stylesheet">
<link href="./font-awesome/4.4.0/css/font-awesome.min.css"  rel="stylesheet" type="text/css">
<script type="text/javascript">
	//全局变量
	var GV = {
		DIMAUB: "/",
		JS_ROOT: "{$STATIC_ROOT}/js/",
		TOKEN: ""
	};
</script>
<style>
	.length_3{width: 180px;}
	form .input-order{margin-bottom: 0px;padding:3px;width:40px;}
	.table-actions{margin-top: 5px; margin-bottom: 5px;padding:0px;}
	.table-list{margin-bottom: 0px;}
</style>
<script src="./js/jquery.js"></script>
<script src="./js/wind.js"></script>

<script src="./js/common.js"></script>

<script src="./bootstrap/js/bootstrap.min.js"></script>
	<style>
		#think_page_trace_open{
			z-index:9999;
		}
	</style><style>.expander{margin-left: -20px;}</style>
</head>
<body>
<div class="wrap js-check-wrap">
	<ul class="nav nav-tabs">
		<li class="active"><a href="javascript:;">权限设置</a></li>
	</ul>
	<form class="js-ajax-form" method="post" >
		<div class="table_full">
			<table width="100%" cellspacing="0" id="dnd-example">
				<tbody>
				{foreach name="$role" item="vo"}
				{php}

				if (in_array($vo['id'],$user_rule)){
				echo "<tr id='node-"
					 .$vo["id"] . "' ><td style='padding-left:30px;'>"
					."<input type='checkbox' name='items' value='".$vo["id"]."' level='0'  checked='true' onclick='javascript:checknode(this);'>"
					. $vo['name'] . "</td></tr>";
				}else{
				echo "<tr id='node-"
					.$vo["id"] . "' ><td style='padding-left:30px;'>"
					."<input type='checkbox' name='items' value='"
					.$vo["id"]. "' level='0'  onclick='javascript:checknode(this);'>"
					. $vo['name'] . "</td></tr>";				}

				foreach($vo['child'] as $v => $v1){
				if (in_array($v1['id'],$user_rule)){
					echo "<tr id='node-"
						.$v1["id"] . "'  class='child-of-node-"
						. $vo["id"]. "'>"
						."<td style='padding-left:30px;'>&nbsp;&nbsp;&nbsp;│ &nbsp;&nbsp;&nbsp;└─ "
						."<input type='checkbox' name='items' checked='true' value='".$v1["id"]."' level='1'  onclick='javascript:checknode(this);'>"
						. $v1['name'] . "</td></tr>";

				}else{
					echo "<tr id='node-"
						. $v1["id"] . "'  class='child-of-node-"
						. $vo["id"]. "'>"
						."<td style='padding-left:30px;'>&nbsp;&nbsp;&nbsp;│ &nbsp;&nbsp;&nbsp;└─ "
						."<input type='checkbox' name='items' value='".$v1["id"]."' level='1'  onclick='javascript:checknode(this);'>"
						. $v1['name'] . "</td></tr>";
					}
				}
				{/php}
				{/foreach}
			</table>
		</div>
		<div class="form-actions">
			<input type="hidden" name="roleid" value="2" />
			<button class="btn btn-primary " id="btn" type="button">保存</button>
			<!--<a class="btn" href="">返回</a>-->
		</div>
	</form>
</div>

<script type="application/javascript">
	$("#btn").click(function(){

		var obj=document.getElementsByName('items');  //选择所有name="'test'"的对象，返回数组
		//取到对象数组后，我们来循环检测它是不是被选中
		var text='';
		for(var i=0; i<obj.length; i++){
			if(obj[i].checked) text+=obj[i].value+',';  //如果选中，将value添加到变量s中
		}

		$.post("{:admin_url('/admin/index/role_auth')}",
				{
					id:"{$id}",
					auth:text
				},
				function(data){
					if(data == 0){
						alert("失败")
					}else if(data == 1){
						alert("成功")
						location="{:admin_url('/admin/index/rbac')}"
					}
				}
		)
	})
</script>

<script type="text/javascript">

	var ajaxForm_list = $('form.js-ajax-form');
	if (ajaxForm_list.length) {
		Wind.use('ajaxForm', 'artDialog', function () {
			if ($.browser.msie) {
				//ie8及以下，表单中只有一个可见的input:text时，会整个页面会跳转提交
				ajaxForm_list.on('submit', function (e) {
					//表单中只有一个可见的input:text时，enter提交无效
					e.preventDefault();
				});
			}

			$('button.js-ajax-submit').bind('click', function (e) {
				e.preventDefault();
				/*var btn = $(this).find('button.js-ajax-submit'),
				 form = $(this);*/
				var btn = $(this),
						form = btn.parents('form.js-ajax-form');

				//批量操作 判断选项
				if (btn.data('subcheck')) {
					btn.parent().find('span').remove();
					if (form.find('input.js-check:checked').length) {
						var msg = btn.data('msg');
						if (msg) {
							art.dialog({
								id: 'warning',
								icon: 'warning',
								content: btn.data('msg'),
								cancelVal: "关闭",
								cancel: function () {
									btn.data('subcheck', false);
									btn.click();
								}
							});
						} else {
							btn.data('subcheck', false);
							btn.click();
						}

					} else {
						$('<span class="tips_error">请至少选择一项</span>').appendTo(btn.parent()).fadeIn('fast');
					}
					return false;
				}

				//ie处理placeholder提交问题
				if ($.browser.msie) {
					form.find('[placeholder]').each(function () {
						var input = $(this);
						if (input.val() == input.attr('placeholder')) {
							input.val('');
						}
					});
				}

				form.ajaxSubmit({
					url: btn.data('action') ? btn.data('action') : form.attr('action'),
					//按钮上是否自定义提交地址(多按钮情况)
					dataType: 'json',
					beforeSubmit: function (arr, $form, options) {
						var text = btn.text();

						//按钮文案、状态修改
						btn.text(text + '中...').attr('disabled', true).addClass('disabled');
					},
					success: function (data, statusText, xhr, $form) {
						var text = btn.text();

						//按钮文案、状态修改
						btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();

						if (data.state === 'success') {
							$('<span class="tips_success">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('slow').delay(1000).fadeOut(function () {
								if (data.referer) {
									//返回带跳转地址
									if (window.parent.art) {
										//iframe弹出页
										window.parent.location.href = data.referer;
									} else {
										window.location.href = data.referer;
									}
								} else {
									if (window.parent.art) {
										reloadPage(window.parent);
									} else {
										//刷新当前页
										reloadPage(window);
									}
								}
							});
						} else if (data.state === 'fail') {
							$('<span class="tips_error">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('fast');
							btn.removeProp('disabled').removeClass('disabled');
						}
					}
				});
			});

		});
	}
	$(document).ready(function () {
		Wind.css('treeTable');
		Wind.use('treeTable', function () {
			$("#dnd-example").treeTable({
				indent: 20
			});
		});
	});

	function checknode(obj) {
		var chk = $("input[type='checkbox']");
		var count = chk.length;
		var num = chk.index(obj);
		var level_top = level_bottom = chk.eq(num).attr('level');
		for (var i = num; i >= 0; i--) {
			var le = chk.eq(i).attr('level');
			if (eval(le) < eval(level_top)) {
				chk.eq(i).attr("checked", true);
				var level_top = level_top - 1;
			}
		}
		for (var j = num + 1; j < count; j++) {
			var le = chk.eq(j).attr('level');
			if (chk.eq(num).attr("checked") == "checked") {
				if (eval(le) > eval(level_bottom)){
					chk.eq(j).attr("checked", true);
				}
				else if (eval(le) == eval(level_bottom)){
					break;
				}
			} else {
				if (eval(le) > eval(level_bottom)){
					chk.eq(j).attr("checked", false);
				}else if(eval(le) == eval(level_bottom)){
					break;
				}
			}
		}
	}
</script>
</body>
</html>
